<template>
  <div class="wrap-item">
    <!-- 搜索-->
    <el-form ref="formSearch" :model="formSearch" size="small" inline>
      <el-form-item>
        <el-input
          v-model.trim="formSearch.param.typeName"
          placeholder="请输入流程类型名称"
          maxlength="20"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <span class="wrap-btn wrap-primary" @click="search">查询</span>
        <span
          class="wrap-btn wrap-success"
          v-if="$checkPermission(101)"
          @click="add"
          >添加流程类型</span
        >
      </el-form-item>
    </el-form>
    <!-- 数据列表 -->
    <div class="wrap-item-roll">
      <el-table
        :data="tableData"
        :header-cell-style="{ background: '#F6F7F9' }"
        row-key="id"
        default-expand-all
        :tree-props="{ children: 'childList', hasChildren: 'hasChildren' }"
      >
        <el-table-column
          type="index"
          width="50"
          label="序号"
          align="center"
        ></el-table-column>
        <el-table-column prop="typeName" label="类型名称"></el-table-column>
        <el-table-column prop="sysUserName" label="创建人"></el-table-column>
        <el-table-column prop="createTime" label="创建时间"
          ><template slot-scope="scope">{{
            scope.row.createTime
          }}</template></el-table-column
        >
        <el-table-column prop="updateTime" label="最后更新时间"
          ><template slot-scope="scope">{{
            scope.row.updateTime
          }}</template></el-table-column
        >
        <el-table-column
          prop="content"
          label="操作"
          width="80px"
          align="center"
        >
          <template slot-scope="scope">
            <el-dropdown @command="(command) => handle(command, scope.row)">
              <span class="el-dropdown-link">
                更多
                <i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="view" v-if="$checkPermission(104)"
                  >查看</el-dropdown-item
                >
                <el-dropdown-item command="edit" v-if="$checkPermission(102)"
                  >编辑</el-dropdown-item
                >
                <el-dropdown-item command="remove" v-if="$checkPermission(103)"
                  >删除</el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <web-page
      :page="formSearch.pageNum"
      :size="formSearch.pageSize"
      :total="total"
      @sizeChange="sizeChange"
      @currentChange="currentChange"
    ></web-page>
    <!-- 对话框 -->
    <el-drawer :title="title" :wrapperClosable="false" :visible.sync="visible">
      <el-form label-width="80px" :model="form" size="medium">
        <el-form-item label="类型名称" required>
          <el-input
            v-model.trim="form.typeName"
            clearable
            placeholder="请输入流程类型名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="父级类型">
          <el-tree
            ref="tree"
            node-key="id"
            show-checkbox
            :accordion="true"
            :data="tableData"
            :props="{ children: 'childList', label: 'typeName' }"
            :check-strictly="true"
            @check="check"
            style="margin-top: 8px !important"
          ></el-tree>
        </el-form-item>
        <el-form-item label="描述">
          <el-input
            type="textarea"
            v-model.trim="form.description"
            placeholder="请输入流程类型描述"
            maxlength="200"
            resize="none"
          ></el-input>
        </el-form-item>
        <el-form-item style="text-align: end">
          <span class="wrap-btn wrap-success" @click="save">确定</span>
          <span class="wrap-btn wrap-default" @click="visible = false"
            >取消</span
          >
        </el-form-item>
      </el-form>
    </el-drawer>
    <!-- 查看 -->
    <el-dialog title="查看" :visible.sync="isLook" width="888px">
      <el-descriptions :column="2" border>
        <el-descriptions-item label="流程类型名称">
          {{ lockData.typeName }}
        </el-descriptions-item>
        <el-descriptions-item label="创建人">
          {{ lockData.sysUserName }}
        </el-descriptions-item>
        <el-descriptions-item label="创建时间">
          {{ lockData.createTime }}
        </el-descriptions-item>
        <el-descriptions-item label="最后更新时间">
          {{ lockData.updateTime }}
        </el-descriptions-item>
        <el-descriptions-item label="流程类型描述" :span="2">
          <el-tooltip
            popper-class="ellipsis-tip-span"
            :content="lockData.description"
            placement="top-start"
          >
            <span class="ellipsis-span">{{ lockData.description }}</span>
          </el-tooltip>
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      formSearch: {
        pageNum: 1,
        pageSize: 10,
        param: {},
      },
      total: null,
      tableData: [],
      visible: false,
      title: '添加流程类型',
      form: {},
      isLook: false,
      lockData: {},
    }
  },
  created() {
    this.itemList()
  },
  methods: {
    itemList() {
      // 获取列表
      this.$axios
        .post('/workorder/configWorkOrderTypeManage/page', this.formSearch)
        .then((data) => {
          this.tableData = []
          if (data.code == 200) {
            this.tableData = data.data.list
            this.total = data.data.total
          } else {
            this.$message.error(data.msg)
          }
        })
    },
    sizeChange(val) {
      //每页条数
      this.formSearch.pageSize = val
      this.itemList()
    },
    currentChange(val) {
      //分页
      this.formSearch.pageNum = val
      this.itemList()
    },
    search() {
      //搜索
      this.formSearch.pageNum = 1
      this.itemList()
    },
    add() {
      //添加
      this.form = {}
      this.title = '添加流程类型'
      this.$nextTick(function () {
        this.$refs.tree.setCheckedKeys([])
      })
      this.visible = true
    },
    handle(command, row) {
      //操作
      if (!command) {
        this.$message.error('菜单选项缺少command属性')
        return
      }
      switch (command) {
        case 'view':
          this.$axios
            .get('/workorder/configWorkOrderTypeManage/detail?id=' + row.id)
            .then((data) => {
              this.lockData = {}
              if (data.code == 200) {
                this.lockData = data.data
                this.isLook = true
              } else {
                this.$message.error(data.msg)
              }
            })
          break
        case 'edit':
          this.title = '编辑流程类型'
          this.$axios
            .get('/workorder/configWorkOrderTypeManage/detail?id=' + row.id)
            .then((data) => {
              this.form = {}
              if (data.code == 200) {
                this.form = data.data
                this.$nextTick(function () {
                  this.$refs.tree.setCheckedKeys([data.data.parentNo])
                })
                this.visible = true
              } else {
                this.$message.error(data.msg)
              }
            })
          break
        case 'remove':
          //删除
          this.$confirm('您确定要删除该条数据吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          })
            .then(() => {
              this.$axios
                .get('/workorder/configWorkOrderTypeManage/delete?id=' + row.id)
                .then((data) => {
                  if (data.code == 200) {
                    this.itemList()
                    this.$message.success('操作成功！')
                  } else {
                    this.$message.error(data.msg)
                  }
                })
            })
            .catch(() => {})
          break
      }
    },
    save() {
      //保存
      if (!this.form.typeName) {
        this.$message.error('请输入流程类型名称')
        return
      }
      const node = this.$refs.tree.getCheckedNodes()
      this.form.parentNo = node.length == 0 ? 0 : node[0].id
      if (this.form.id == this.form.parentNo) {
        this.$message.error('父级类型不能是类型名称')
        return
      }
      this.$axios
        .post('/workorder/configWorkOrderTypeManage/save', this.form)
        .then((data) => {
          if (data.code == 200) {
            this.visible = false
            this.$message.success('操作成功！')
            this.itemList()
          } else {
            this.$message.error(data.msg)
          }
        })
    },
    check(a, b) {
      //监听checkbox改变事件，从而控制只选择一个节点
      if (b.checkedKeys.length > 0) {
        this.$refs.tree.setCheckedKeys([a.id])
      }
    },
  },
}
</script>
